from django.db import models

# Create your models here.


# 书 籍 的 模 型 类
class BookInfo(models.Model):
    #图书名称
    name = models.CharField(max_length=20,verbose_name='图书名称')
    pub_date = models.DateField(verbose_name='发布日期', null=True)
    readcount = models.IntegerField(default=0, verbose_name='阅读量')
    commentcount = models.IntegerField(default=0, verbose_name='评论量')
    is_delete = models.BooleanField(default=False, verbose_name='逻辑删除')

    class Meta:
        db_table = 'bookinfo' #表的名字
        verbose_name = '书籍信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        """将模型类以字符串的方式输出"""
        return self.name

# 人物  模型类
class PeopleInfo(models.Model):
    GENDER_CHOICES = (
        (0, '女'),
        (1, '男')
    )
    #人物名字
    name = models.CharField(max_length=20,verbose_name='人物名字')
    #人物性别
    gender = models.BooleanField(choices=GENDER_CHOICES, default=0, verbose_name='性别')
    description = models.CharField(max_length=200, null=True, verbose_name='描述信息')
    book = models.ForeignKey(BookInfo, on_delete=models.CASCADE, verbose_name='图书')  # 外键
    is_delete = models.BooleanField(default=False, verbose_name='逻辑删除')

    # 外键约束：人物属于哪本书
    #on_delete=models.CASCADE：级联删除，一被删除，多也被删除
    book = models.ForeignKey(BookInfo,on_delete=models.CASCADE)

    class Meta:
        db_table = 'peopleinfo'
        verbose_name = '人物信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        """将模型类以字符串的方式输出"""
        return self.name
